#include "game.h"
#include "config.h"

#include "defaultsetting.h"

Game* Game::m_self=0;

Game& Game::self()
{
  if(m_self==0)
    m_self=new Game();
  return *m_self;
}

Game::~Game()
{
}

/*
Render* Game::getRender()const
{
  return *m_render;
}

TextureLoader* Game::getTextureLoader()const
{
  return *m_textureLoader;
}

AudioPlayer* Game::getAudioPlayer()const
{
  return *m_audioPlayer;
}

AudioLoader* Game::getAudioLoader()const
{
  return *m_soundLoader;
}
*/

InputDevices& Game::getInputDevices()const
{
  return *m_inputDevices;
}

Config& Game::getSetting()const
{
  return *m_setting;
}

bool Game::load()
{
  return m_timeSystem->load();
  /*
  string engine=(*m_setting)["render"].readValue("engine",DEFAULT_RENDER);
  int enableSound=(*m_setting)["sound"].readValue("enable",DEFAULT_ENABLE_SOUND);
  int enableJoystick=(*m_setting)["input"].readValue("enable_joystick",DEFAULT_ENABLE_JOYSTICK);

  if(engine=="SDL")
  {
    m_render=new RenderSDL();
    m_textureLoader=new TextureLoaderSDL();
  }

  else if(engine=="OpenGL")
  {
    m_render=new RenderOpenGL();
    m_textureLoader=new TextureLoaderOpenGL();
  }

  else
    // FIXME Mandar mensajes de error
    return false;

  int flags=SDL_INIT_VIDEO | SDL_INIT_TIMER;

  if(enableSound)
    flags|=SDL_INIT_SOUND;
  if(enableJoystick)
    flags|=SDL_INIT_JOYSTICK;

  if(SDL_Init(flags)!=0)
  {

    // FIXME Mandar mensajes de error
    return false;
  }

  m_soundLoader=new AudioLoader();
  m_inputDevices=new InputDevices();
  m_timeSystem=new TimeSystem();

  if(!m_render->load())
  {
  }
  else if(!m_audioPlayer->load())
  {
  }

  setWindowNameIcon();

  return true;
  */
}

void Game::setWindowNameIcon()
{
  string name=(*m_setting)["general"].readEntry("name","FrameWork Test");
  string icon=(*m_setting)["general"].readEntry("icon","");

  SDL_WM_SetCaption(name.c_str(),NULL);

  // FIXME!!
  if(icon!="")
  {
    //SDL_Surface* s=IMG_Load(icon.c_str());
    //SDL_WM_SetIcon(s,NULL);
    //SDL_FreeSurface(s);
  }
}

//TODO
bool Game::reload()
{
  setWindowNameIcon();
}

void Game::start()
{
  while(!m_end && !m_scene.empty())
  {
    if(m_timeSystem->isLogicTick())
    {
      //m_inputSystem->update();
      getActualScene().logicTick();
    }
    if(m_timeSystem->isGraphicTick())
    {
      getActualScene().graphicTick();
      //m_render->draw();
      //m_audioPlayer->play();
    }
  }

  unload();
}

inline Scene& Game::getActualScene()const
{
  /* FIXME
  if(m_scene.empty())
    m_scene.push(new Scene());
    */

  return *(m_scene.front());
}

inline void Game::pushScene(Scene* sc)
{
  /* FIXME
  m_scene.push(sc);
  */
}

inline void Game::popScene()
{
  /* FIXME
  if(!m_scene.empty())
  {
    delete m_scene.front();
    m_scene.pop();
  }
  */
}

inline unsigned Game::getSceneNumber()const
{
  return m_scene.size();
}

Game::Game():
  //m_render(0),m_textureLoader(0),m_audioPlayer(0),
             //m_soundLoader(0),m_timeSystem(0),m_end(false)
  m_end(false)
{
  m_timeSystem=new TimeSystem();
  m_setting=new Config();
}

// FIXME comprobar si hay que grabar los setting
void Game::unload()
{
  /*
  delete m_render;
  delete m_textureLoader;
  delete m_soundLoader;
  delete m_inputSystem;
  */
  delete m_timeSystem;
  delete m_setting;

  while(!m_scene.empty())
  {
    popScene();
  }

  SDL_Quit();
}
